
package com.jf.cloud.combo.controller.multishop;

import com.jf.cloud.combo.dto.ComboSpuDTO;
import com.jf.cloud.combo.model.ComboSpu;
import com.jf.cloud.combo.service.ComboSpuService;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.product.vo.ComboSpuVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 套装商品项
 *
 * @author zz
 * @date 2022-08-16 09:18:06
 */
@RestController("multishopComboProdController")
@RequestMapping("/m/combo_prod")
@Tag(name = "套装商品项")
public class ComboProdController {

    @Autowired
    private ComboSpuService comboProdService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取套装商品项列表" , description = "分页获取套装商品项列表")
    public ServerResponseEntity<PageVO<ComboSpu>> page(@Valid PageDTO pageDTO) {
        PageVO<ComboSpu> comboProdPage = comboProdService.page(pageDTO);
        return ServerResponseEntity.success(comboProdPage);
    }

    @GetMapping
    @Operation(summary = "获取套装商品项" , description = "根据comboProdId获取套装商品项")
    public ServerResponseEntity<ComboSpuVO> getByComboProdId(@RequestParam Long comboProdId) {
        return ServerResponseEntity.success(comboProdService.getByComboSpuId(comboProdId));
    }

    @PostMapping
    @Operation(summary = "保存套装商品项" , description = "保存套装商品项")
    public ServerResponseEntity<Void> save(@Valid @RequestBody ComboSpuDTO comboProdDTO) {
        ComboSpu comboProd = mapperFacade.map(comboProdDTO, ComboSpu.class);
        comboProd.setComboSpuId(null);
        comboProdService.save(comboProd);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新套装商品项" , description = "更新套装商品项")
    public ServerResponseEntity<Void> update(@Valid @RequestBody ComboSpuDTO comboProdDTO) {
        ComboSpu comboProd = mapperFacade.map(comboProdDTO, ComboSpu.class);
        comboProdService.update(comboProd);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除套装商品项" , description = "根据套装商品项id删除套装商品项")
    public ServerResponseEntity<Void> delete(@RequestParam Long comboProdId) {
        comboProdService.deleteById(comboProdId);
        return ServerResponseEntity.success();
    }
}
